package com.dyp.h2_demo.ctr;

import com.dyp.h2_demo.entity.Person;
import com.dyp.h2_demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.util.Random;

@RestController
@RequestMapping(value = "/person")
public class PersonCtr {
    @Autowired
    private PersonService personService;

    @GetMapping("/info")
    public String hello() {
        Optional<Person> person = personService.findById(1);
        return person.toString();
    }

    @PutMapping("/{id}")
    public String update(@PathVariable("id") int id) {
        Person person = personService.findById(id).get();
        // 设置一个随机值
        person.setName(new Random().nextInt(1000) + "");
        personService.updateById(person.getId(), person.getName() );
        return  person.toString();
    }
}
